from datetime import datetime
from typing import List


class Executor:
    def __init__(self,start_date:datetime,end_date:datetime,cash:float) -> None:
        self.start_date = start_date
        self.end_date = end_date
        self.cash = cash
        self.position = []
        
    def buy(self,stock_code,price,amount)->None:
        target_stock_index = next((index for index,stock in enumerate(self.position) if stock["stock_code"] == stock_code), None)
        self.cash -= price*amount
        if target_stock_index is not None:
            self.position[target_stock_index]["amount"] += amount
        else:
            self.position.append({
                "stock_code":stock_code,
                "amount":amount
            })
    
    def sell(self,stock_code,price,amount)->None:
        target_stock_index = next((index for index,stock in enumerate(self.position) if stock["stock_code"] == stock_code), None)
        self.cash += price*amount
        if target_stock_index is not None:
            self.position[target_stock_index]["amount"] -= amount
        else:
            return False

    def executeOperation(self,stock_code:str,operation_type:str,price:float,amount:int)->None:
        if operation_type == "BUY":
            self.buy(stock_code,price,amount)
        else:
            self.sell(stock_code,price,amount)